package com.share.lottery.utils;

import java.util.ArrayList;
import java.util.List;

/***
 * 贷款，税率的工具类
 * 
 * @author lmj
 * 
 */
public class CounterUtils {
	/********************************* 房贷，税费相关 *****************************************************************/
	// 商业贷款利率
	public static final double RATE1 = 6.55;
	// 公积金贷款利率
	public static final double RATE2 = 4.50;

	// 印花税 都按照百分比来
	public static final double YIN_HUA_SHUI = 0.05;
	// 公证费
	public static final double GONG_ZHENG_FEI = 0.3;
	// 契税
	public static final double QI_YUE_FEI = 1.5;
	// 委托办理产权手续
	public static final double WEI_TUO_FEI = 0.3;
	// 房屋买卖手续费
	public static final double MAI_MAI_FEI = 3;

//	public static List<Double> objArray = new ArrayList<Double>();
//	// 月还款额
//	public static Double interestM = (double) 0;
//	// 累计还款总额
//	public static Double interestTotal = (double) 0;

	/**
	 * 等额本息还款法
	 * 
	 * @param original贷款金额
	 * @param yearratio
	 *            yearratio年利率%，如年利率5.6%就为5.6
	 * @param year还款年限
	 * 
	 * @return
	 */
	public static List<Double> averageCapital(double original,
			double yearratio, int year) {

		// 还款月数
		int timeSpan = year * 12;
		// 某种利率
		double active = yearratio * 10 / 12 * 0.001;

		 List<Double> objArray = new ArrayList<Double>();
		// 月还款额
		 Double interestM = (double) 0;
		// 累计还款总额
		 Double interestTotal = (double) 0;

		for (int i = 1; i < timeSpan + 1; i++) {
			double t1 = (original - original * (i - 1) / timeSpan) * active;// 第i月还款利息
			interestM = original / timeSpan + t1;// 第i月还款额

			objArray.add(interestM);
			interestTotal = interestTotal + interestM;
		}
		interestTotal = roundDouble(interestTotal,2);//(double) ((Math.round(interestTotal * 100)) / 100);
		objArray.add(timeSpan, interestTotal);
		return objArray;
	}

	/**
	 * 总金额
	 * 
	 * @param allRepayment
	 * @param moneyRate
	 * @param yearValue
	 * @return
	 */
	public static double countAll(int type, double allRepayment,
			double moneyRate, int yearValue) {
		double all = 0;

		if (type == 0) {
			all = ((allRepayment * moneyRate / 1200
					* Math.pow(1 + moneyRate / 1200, yearValue * 12)
					* yearValue * 12) / (Math.pow(1 + moneyRate / 1200,
					yearValue * 12) - 1));
		} else {// 等额本金
			all = allRepayment + allRepayment * moneyRate / 1200
					* (1 + yearValue * 12) / 2;
			
//			all = averageCapital(allRepayment,moneyRate,yearValue);
		}

		return all;
	}

	/**
	 * 获取小数点后两位
	 * 
	 * @param result_value
	 * @return
	 */
	public static double makeDoule2(double result_value) {

		double result = (double) (Math.round(result_value * 100) / 100.0);

		return result;
	}

	/**
	 * 
	 * 普 宅：90平米以下：1%、90-140平米：1.5% 143平米以上或是非普宅：3%、商业房或公司产权：3%
	 * 
	 * @param type
	 *            0普通住宅
	 * @return
	 */
	public static double getQiShuiByType(int type, double space) {
		double qishui = 3;

		switch (type) {// 普通住宅需要判断面积
		case 0:
			if (space < 90) {
				qishui = 1;
			} else if (space >= 90 && space < 140) {
				qishui = 1.5;
			} else {
				qishui = 3;
			}
			break;
		case 1:// 非普通住宅
			qishui = 3;
			break;
		case 2:
			qishui = 0;
			break;

		default:
			break;
		}

		return qishui;
	}

    /** 
     * 小数 四舍五入 
     *  
     * @param val 
     * @param precision 
     * @return 
     */  
      
    public static Double roundDouble(double val, int precision)  
    {  
     Double ret = null;  
     try  
     {  
      double factor = Math.pow(10, precision);  
      ret = Math.floor(val * factor + 0.5) / factor;  
     }  
     catch (Exception e)  
     {  
      e.printStackTrace();  
     }  
     return ret;  
    }  
	
}
